package com.learn.sunday.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class LoginFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("执行自定义的全局过滤器");
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        RequestPath path = request.getPath();
        log.info("{}:{}没有登录{}", path,request.getRemoteAddress());
//        if ("/VIID/System/Register".equals(path.toString())) {
//            return chain.filter(exchange);
//        }
//        String token = request.getQueryParams().getFirst("access-token");
//        if (StringUtils.isBlank(token)) {
//            InetSocketAddress remoteAddress = request.getRemoteAddress();
//            InetSocketAddress localAddress = request.getLocalAddress();
//            log.info("{}:{}没有登录{}", path,remoteAddress.getPort(),localAddress.getPort());
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
